

$(function() {
	//加载表格树数据
	loadTreeGridData();
	
	//表格上方新增按钮点击事件
	$('#btnAdd').on('click', function(){
		$("#layer_opt_status").val("");
		showDialogUrl('新增模块', 800,  450, 
			rootPath + '/siteModule/toAddSiteModule',
			function(){
				var layer_opt_status = $("#layer_opt_status").val();
				if (layer_opt_status == "1") {
					loadTreeGridData();
				}
			}
		);
	})
	//查询
	$('#btnQuery').on('click', function(){
		loadTreeGridData();
	})
	//清空
	$('#btnClear').on('click', function(){
		$('#moduleName').val("");
	})
})
/**
 * 加载表格树数据
 */
var loadTreeGridData = function(){
	var url = rootPath + "/siteModule/listSiteModulePage";
	var moduleName = $('#moduleName').val();   
	$.ajax({
		type: 'POST',
		url: url,
		async: true,
		data: {moduleName: moduleName},
		dataType: 'json',
		success: function(data) {
			if (data) {
				initTreeGrid(data.rows);
			}
		}
	})
}
/**
 * 初始化表格树
 * @param rows 行JSON数据集合
 */
var initTreeGrid = function(rows) {
	// prepare the data
	var source = { 
		dataType : "json", 
		dataFields : [ 
		    { name : 'id', type : 'number' }, 
		    { name : 'moduleCode', type : 'string' }, 
		    { name : 'moduleName', type : 'string' }, 
		    { name : 'parentModuleId', type : 'number' }, 
		    { name : 'moduleStatus', type : 'string' }, 
		    { name : 'allowDelete', type : 'string' }, 
		    { name : 'intro', type : 'string' }
		],
		hierarchy : {
			keyDataField : { name : 'id' },
			parentDataField : { name : 'parentModuleId' }
		},
		id : 'id',
		localData : rows
	};

	var dataAdapter = new $.jqx.dataAdapter(source);
	// create Tree Grid
	$("#treeGrid").jqxTreeGrid(
	{
		width: '100%',
		columnsHeight: 40,
		autoRowHeight: true,
		columnsResize: true,
		altRows: true,
		source: dataAdapter,
		theme: 'office',
		sortable: false,
		columns: [
		   { text: '模块名称', dataField: 'moduleName', width: 400, align: "center" },
	       { text: '模块备注', dataField: 'intro', width: 200, align: "center", cellsalign: "center" },
	       { text: '状态', dataField: 'moduleStatus', width: 150, align: "center", cellsalign: "center",
	    	   cellsrenderer: function (row, columnfield, value, defaulthtml, columnproperties) {
	    		   if (value == '0') {
	    			   return "<span class='label label-danger'>停用</span>";
	    		   } else {
	    			   return "<span class='label label-success'>启用</span>";
	    		   }
	           }   
	       },
	       { text: '操作', dataField: 'id', align: "center", cellsalign: "center",
	    	   cellsrenderer: function (row, column, value, rowData) {
	    		   var strHtml = "";
	    		   if (rowData.allowDelete != '1'){
	    			   if (rowData.moduleStatus == '0') {
		    			   strHtml += "<a class='btn btn-primary btn-xs btn-category-status' bind-id='"+rowData.id+"' bind-data='"+rowData.moduleStatus+"'>启用</a>";
		    		   } else {
		    			   strHtml += "<a class='btn btn-danger btn-xs btn-category-status' bind-id='"+rowData.id+"' bind-data='"+rowData.moduleStatus+"'>停用</a>";
		    		   }
	    		   }
	    		   strHtml += "<a class='btn btn-success btn-xs btn-add-category'  bind-data='"+rowData.id+"'>新增</a>";
	    		   strHtml += "<a class='btn btn-warning btn-xs btn-edit-category' bind-data='"+rowData.id+"'>编辑</a>";
	    		   if (rowData.allowDelete != '1'){
	    			   strHtml += "<a class='btn btn-danger btn-xs btn-remove-category'bind-data='"+rowData.id+"'>删除</a>";
	    		   }
	               return strHtml;
	           }
	       }
	    ],
	    rendered: function() {
	    	//新增
	    	$('.btn-add-category').on('click', function(){
	    		var parentModuleId = $(this).attr('bind-data');
	    		if (!parentModuleId) return;
	    		$("#layer_opt_status").val(""); //初始化弹出框操作状态
	    		showDialogUrl('新增模块', 800, 450, rootPath + '/siteModule/toAddSiteModule?parentModuleId='+parentModuleId,
	    			function(){
						var layer_opt_status = $("#layer_opt_status").val(); //获取弹出框操作状态(确定1 取消 0)
						if (layer_opt_status == "1") { //如果是确定
							loadTreeGridData();
						}
					}
	    		);
	    	});
	    	//编辑
	    	$('.btn-edit-category').on('click', function(){
	    		var id = $(this).attr('bind-data');
	    		if (!id) return;
	    		$("#layer_opt_status").val(""); //初始化弹出框操作状态
	    		var index = showDialogUrl('编辑分类', 800, 450, rootPath + '/siteModule/toUpdateSiteModule?id='+id,
	    			function(){
						var layer_opt_status = $("#layer_opt_status").val(); //获取弹出框操作状态(确定1 取消 0)
						if (layer_opt_status == "1") { //如果是确定
							loadTreeGridData();
						}
					}
	    		);
	    		layer.full(index);
	    	});
	    	//删除
	    	$('.btn-remove-category').on('click', function(){
	    		var id = $(this).attr('bind-data');
	    		layer.confirm("确定要删除？", {
	    				btn: ['确定','取消'], //按钮
	    				skin: 'layui-layer-lan' //样式类名
					}, function(index){
						$.ajax({
    						type: 'POST',
    						url: rootPath + "/siteModule/deleteSiteModuleById",
    						async: false,
    						data: {id: id},
    						dataType: 'json',
    						success: function(data) {
    							if (data.resultCode == '1') {
    								loadTreeGridData();
    							} else {
    								layer.msg(data.msg, {icon: 2});
    							}
    						}
    					});
						layer.close(index);
					} 
	    		);
	    	});
	    	//状态停用/启用
	    	$('.btn-category-status').on('click', function(){
	    		var id = $(this).attr('bind-id');  
	    		var status = $(this).attr('bind-data');  
	    		var confirmInfo = "";
	    		if (status == '0') {
	    			confirmInfo = "您确定要启用？";
	    		} else {
	    			confirmInfo = "您确定要停用？";
	    		}
	    		layer.confirm(confirmInfo, {
	    				btn: ['确定','取消'], //按钮
	    				skin: 'layui-layer-lan' //样式类名
    				}, function(index){
    					$.ajax({
    						type: 'POST',
    						url: rootPath + "/siteModule/saveOrUpdateSiteModule",
    						async: false,
    						data: {id: id, moduleStatus: status == '0' ? '1' : '0'},
    						dataType: 'json',
    						success: function(data) {
    							if (data.resultCode == '1') {
    								loadTreeGridData();
    							}
    						}
    					});
    					layer.close(index);
    				} 
	    		);
	    		 
	    	});
	    	
	    }
	});
	
	$("#treeGrid").jqxTreeGrid('expandAll');
}

 